package com.yiren.backstage.player.api;

import android.content.Context;

import com.yiren.backstage.currency.bean.VoiceBean;
import com.yiren.backstage.player.announcer.QueueMediaPlayer;
import com.yiren.backstage.player.announcer.SingleMediaPlayer;
import com.yiren.backstage.player.services.PlayVoiceService;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/12/29 10:52
 * Author:xiaozan
 * Description:播报器 api
 */
public class PlayerApi {

    //单例播报器的队列语音集合
    private static List<VoiceBean> singleQueueAllVoiceFiles = new ArrayList<>();

    //队列播报器的队列语音集合
    private static List<VoiceBean> queueAllVoiceFiles = new ArrayList<>();

    /**
     * 播报项目或者评判语音  播报优先级最高
     *
     * @param context
     * @param voiceFile       语音对象
     * @param iPlayerCallBack 回调
     */
    public static void playProjectAndJudgeVoice(Context context, VoiceBean voiceFile, IPlayerCallBack iPlayerCallBack) {
        //关闭队列播报器
        QueueMediaPlayer.getInstance().reset();
        queueAllVoiceFiles.clear();

        //将播报语音加入队列中：当前有语音播报时有新语音加入，队列播报
        if (null != voiceFile) {
            if (null != iPlayerCallBack) voiceFile.setiPlayerCallBack(iPlayerCallBack);
            singleQueueAllVoiceFiles.add(voiceFile);
        }

        if (singleQueueAllVoiceFiles.size() == 0) return;
        if (SingleMediaPlayer.getInstance().isPlaying()) return;

        PlayVoiceService.mediaPlayVoice(context, singleQueueAllVoiceFiles.get(0), () -> {
            //递归播报语音
            if (singleQueueAllVoiceFiles.size() > 0) {
                if (null != singleQueueAllVoiceFiles.get(0).getiPlayerCallBack())
                    singleQueueAllVoiceFiles.get(0).getiPlayerCallBack().playFinish();
                singleQueueAllVoiceFiles.remove(0);
            }
            playProjectAndJudgeVoice(context, null, null);
        }, SingleMediaPlayer.getInstance());
    }


    /**
     * 播报成绩
     *
     * @param context
     * @param voiceFiles
     * @param iPlayerCallBack
     */
    public static void playScoreVoice(Context context, List<VoiceBean> voiceFiles, IPlayerCallBack iPlayerCallBack) {
        if (null != voiceFiles && voiceFiles.size() > 0) {
            queueAllVoiceFiles.addAll(voiceFiles);
        }

        //判断当前单例模式播报器是否在播报，如果在播报就终结
        if (SingleMediaPlayer.getInstance().isPlaying()) {
            queueAllVoiceFiles.clear();
            if (null != iPlayerCallBack) iPlayerCallBack.playFinish();
            return;
        }
        if (null == queueAllVoiceFiles || queueAllVoiceFiles.size() == 0) {
            if (null != iPlayerCallBack) iPlayerCallBack.playFinish();
            return;
        }
        if (QueueMediaPlayer.getInstance().isPlaying()) return;
        queueAllVoiceFiles.get(0).setVoiceContent(queueAllVoiceFiles.get(0).getVoiceContent().replace("s", "秒"));
        PlayVoiceService.mediaPlayVoice(context, queueAllVoiceFiles.get(0), () -> {
            //递归播报语音
            if (queueAllVoiceFiles.size() > 0) queueAllVoiceFiles.remove(0);
            playScoreVoice(context, null, iPlayerCallBack);
        }, QueueMediaPlayer.getInstance());
    }

    /**
     * 播报操作提示语音
     *
     * @param context
     * @param content
     * @param iPlayerCallBack
     */
    public static void playTipsVoice(Context context, String content, IPlayerCallBack iPlayerCallBack) {
        queueAllVoiceFiles.clear();
        VoiceBean voiceFile = new VoiceBean();
        voiceFile.setVoiceContent(content);
        PlayVoiceService.mediaPlayVoice(context, voiceFile, iPlayerCallBack, SingleMediaPlayer.getInstance());
    }

    /**
     * 重置播报器
     */
    public static void resetAllPlayer() {
        queueAllVoiceFiles = new ArrayList<>();
        singleQueueAllVoiceFiles = new ArrayList<>();
        PlayVoiceService.resetAllPlayer();
    }

    /**
     * 释放播报器
     */
    public static void releaseAllPlayer() {
        queueAllVoiceFiles = new ArrayList<>();
        singleQueueAllVoiceFiles = new ArrayList<>();
        PlayVoiceService.releaseAllPlayer();
    }
}
